'''
Created on 03-Apr-2010

@author: darsh
'''
from Xlib import X
import Xlib
from PyQt4 import QtCore

MARGIN_LEFT = 10
MARGIN_RIGHT = 0
MARGIN_TOP = 10
MARGIN_BOTTOM = 0

           
class Client(): 
    def __init__(self, window, root, desktop,framewidget,starticonified=False):
        self.window = window
        self.sticky = False
        self.framewidget=framewidget
        self.framewidget.framedwindow=self
        geometry=window.get_geometry()._data
        print window.get_wm_hints()
        print window.get_wm_normal_hints()
        QtCore.QObject.connect(self.framewidget,QtCore.SIGNAL("maximized()"),self.togglemaximize)
        QtCore.QObject.connect(self.framewidget,QtCore.SIGNAL("minimized()"),self.iconify)
        self.width=geometry["width"]
        self.height=geometry["height"]
        self.x=geometry["x"]
        self.y=geometry["y"]
        #wm_hints=window.get_wm_hints()
        #wm_normal_hints=window.get_wm_normal_hints()
        rootgeometry = root.get_geometry()
        self.fullscreenx = rootgeometry._data["width"]
        self.fullscrenny = rootgeometry._data["height"]
        self.max_x = rootgeometry._data["width"] - MARGIN_RIGHT
        self.max_y = rootgeometry._data["height"] - MARGIN_BOTTOM
        self.min_x = MARGIN_LEFT
        self.min_y = MARGIN_TOP
        self.max_width = self.max_x - self.min_x
        self.max_height = self.max_y - self.min_y
        self._windowMask = X.StructureNotifyMask | \
            X.PropertyChangeMask | \
            X.EnterWindowMask | \
            X.FocusChangeMask | \
            X.KeyPressMask
        window.change_attributes(event_mask=self._windowMask)
        self.iconified = False
        self.name = "<no name>"
        self.updateName()
        self.beforemaximizex, self.beforemaximizey, self.beforemaximizewidth, self.beforemaximizeheight = 400, 600, 640, 480
        self.beforefullscreenx, self.beforefullscreeny, self.beforefullscreenwidth, self.beforefullscreenheight = 400, 600, 640, 480
        self.maximized = False
        self.fullscreened = False
        #Place the window with above mentioned properties
        #self.place(self.x,self.y,self.width,self.height,1,None)
        self.desktop = desktop
        #APPLY THE _NET_WM_DESKTOP PROPERTY REQUIRED FOR EWMH SPECIFICATION
        self.frame()
        if starticonified:
            self.iconify()
                        
    def updateName(self):
        try:
            self.name = self.window.get_wm_name()
        except (Xlib.error.BadWindow, Xlib.error.BadValue):
            pass
    
    def frame(self):
        self.framewidget.setGeometry(self.x,self.y-32,self.width,32)
        self.framewidget.show()
        
    def unframe(self):
        self.framewidget.hide()
    
    def info(self):
        """
        Returns information for the given client window
        """
        return dict(
                    name=self.name,
                    x=self.x,
                    y=self.y,
                    width=self.width,
                    height=self.height,
                    id=str(hex(self.window.id)),
                    iconified=self.iconified,
                    fullscreen=self.fullscreened,
                    maximized=self.maximized
                    )
        
    def place(self, x, y, width, height, border, borderColor):
        """
            Places the window at the specified location with the given size.
        """
        self.x, self.y, self.width, self.height = x, y, width, height
        self.window.configure(
                              x=x,
                              y=y,
                              width=width,
                              height=height,
                              border_width=border
                              )
        if borderColor is not None:
            self.window.change_attributes(
                                          border_pixel=borderColor
                                          )
        self.frame()
            
    def __repr__(self):
        return str(self.info())
 
    def focus(self, warp=True):
        if self.iconified:
            self.uniconify()
            self.window.set_input_focus(
                                        X.RevertToPointerRoot,
                                        X.CurrentTime
                                        )
        self.window.configure(
                              stack_mode=X.Above
                              )
        if warp:
            self.window.warp_pointer(self.width / 2, self.height / 2)
                          
    def uniconify(self):
        self.window.map()
        self.framewidget.show()
        self.iconified = False    
    
    def iconify(self):
        self.window.unmap()
        self.framewidget.hide()
        self.iconified = True  
    
    def kill(self):
        self.window.kill_client()   
           
    def toggleiconify(self):
        if self.iconified:
            self.uniconify()
        else:
            self.iconify()
            
    def destroy(self):
        self.window.destroy()
            
    def togglemaximize(self):
        if not self.maximized:
            self.beforemaximizex = self.x
            self.beforemaximizey = self.y
            self.beforemaximizewidth = self.width
            self.beforemaximizeheight = self.height
            self.place(self.min_x, self.min_y, self.max_width, self.max_height, 1, None)
            self.maximized = True
        else:
            self.x = self.beforemaximizex
            self.y = self.beforemaximizey
            self.width = self.beforemaximizewidth
            self.height = self.beforemaximizeheight
            self.place(self.x, self.y, self.width, self.height, 1, None)
            self.maximized = False
       
    def togglefullscreen(self):
        if not self.fullscreened:
            self.beforefullscreenx = self.x
            self.beforefullscreeny = self.y
            self.beforefullscreenwidth = self.width
            self.beforefullscreenheight = self.height
            self.place(0, 0, self.fullscreenx, self.fullscrenny, 1, None)
            self.fullscreened = True
        else:
            self.x = self.beforefullscreenx
            self.y = self.beforefullscreeny
            self.width = self.beforefullscreenwidth
            self.height = self.beforefullscreenheight
            self.place(self.x, self.y, self.width, self.height, 1, None)
            self.fullscreened = False
    
    def moveleft(self, amount):
        self.x = self.x - amount
        self.place(self.x, self.y, self.width, self.height, 1, None)
    
    def moveright(self, amount):
        self.x = self.x + amount
        self.place(self.x, self.y, self.width, self.height, 1, None)
   
    def movetop(self, amount):
        self.y = self.y - amount
        self.place(self.x, self.y, self.width, self.height, 1, None)
   
    def movebottom(self, amount):
        self.y = self.y + amount
        self.place(self.x, self.y, self.width, self.height, 1, None)
    
    def growleft(self, amount):
        self.x = self.x - amount
        self.width += amount
        self.place(self.x, self.y, self.width, self.height, 1, None)
     
    def growright(self, amount):
        #self.x=self.x+amount
        self.width += amount
        self.place(self.x, self.y, self.width, self.height, 1, None)
    
    def growtop(self, amount):
        self.y = self.y - amount
        self.height += amount
        self.place(self.x, self.y, self.width, self.height, 1, None)
    
    def growbottom(self, amount):
        #self.y=self.y+amount
        self.height += amount
        self.place(self.x, self.y, self.width, self.height, 1, None)
        
    def shrinkleft(self, amount):
        #self.x=self.x-amount
        self.width -= amount
        self.place(self.x, self.y, self.width, self.height, 1, None)
     
    def shrinkright(self, amount):
        self.x = self.x + amount
        self.width -= amount
        self.place(self.x, self.y, self.width, self.height, 1, None)
    
    def shrinktop(self, amount):
        #self.y=self.y-amount
        self.height -= amount
        self.place(self.x, self.y, self.width, self.height, 1, None)
    
    def shrinkbottom(self, amount):
        self.y = self.y + amount
        self.height -= amount
        self.place(self.x, self.y, self.width, self.height, 1, None)